<?php
ini_set('max_execution_time', '0');
ini_set('memory_limit', '-1');
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/23
 * Time: 13:33
 */
include_once 'class/Http.php';
$config= include('class/config.php');

//$redis = new \think\cache\driver\Redis($config['redis']);
//$db = new \think\Db($config['db']);
$http = new Http();
$genderList=[1,2];
$baseUrl="47.56.207.234/v3/novel/read?userId=7b4asem4&novelId=";

$booklist = file_get_contents('data/chapter.csv');
$booklist = array_filter(explode("\r",$booklist));

foreach ($booklist as $num =>$book){
    $book = json_decode($book,true);
    $nId =$book['nId'];
    $dirName = "books/".$nId;
    if(!is_dir($dirName)){
        mkdir($dirName);
    }
    foreach ($book['cs'] as $content){
        $cId = $content["cid"];
         $fileName=$dirName."/".$cId.".txt";
        if(!file_exists($fileName) || filesize($fileName) == 0){
            $url = $baseUrl.$nId."&chapterId=".$cId;
            $contentData = $http::curlPost($url);
            $contentData = json_decode($contentData,true);
            if($contentData['status'] == 0 && !empty($contentData['data']['c'])){
                $dataList = $contentData['data']['c'];
                $dataList['content'] = str_ireplace("\n",'',$dataList['content']);
                file_put_contents($fileName,json_encode($dataList,258).PHP_EOL);
                echo "正在抓取【".$dataList['nName']."】 ".$dataList['cName'].PHP_EOL;
            }
        }
    }
}



